Imports Cdm.DataCore.Entities
Imports Cdm.DataCore.CaseNotes
Imports System.Collections.Generic

Partial Class Case_caseAffirmations
    Inherits System.Web.UI.UserControl
    Implements IPermission

    Public ReadOnly Property ControlCollection() As System.Web.UI.ControlCollection Implements IPermission.ControlCollection
        Get
            Return Me.Controls
        End Get
    End Property

    Public ReadOnly Property EntityPermissionType() As Cdm.DataCore.UserSecurity.Permission.PermissionType Implements IPermission.EntityPermissionType
        Get
            Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseAffirmations
        End Get
    End Property

    Public Property PrimaryObject() As Cdm.DataCore.ConfigurableObject Implements IPermission.PrimaryObject
        Get
            Return EntityController.GetParticipant()
        End Get
        Set(ByVal value As Cdm.DataCore.ConfigurableObject)
            Dim pt As Cdm.DataCore.Entities.Participant = value
            BindCheckList()
            SetValues(pt.Affirmations)
            Me.AffirmationsCheckBoxList.Enabled = UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        End Set
    End Property

    Public Sub UpdateAffirmations()

        Dim i As Integer
        Dim pt As Participant = Me.PrimaryObject
        For i = 0 To Me.AffirmationsCheckBoxList.Items.Count - 1
            SetAffirmationValue(AffirmationsCheckBoxList.Items(i).Value, AffirmationsCheckBoxList.Items(i).Selected, pt.Affirmations, pt.Id)
        Next

    End Sub

    Private Sub SetAffirmationValue(ByVal idValue As String, _
                                    ByVal isAffirmed As String, _
                                    ByVal affirmationList As List(Of ParticipantAffirmation), _
                                    ByVal participantId As Integer)

        Dim i As Integer
        Dim id As Integer
        id = Convert.ToInt32(idValue)
        For i = 0 To affirmationList.Count - 1
            If affirmationList(i).AffirmationId = id Then
                affirmationList(i).IsAffirmed = isAffirmed
                affirmationList(i).DateModified = System.DateTime.Now
                affirmationList(i).LastModified = UserAuthorization.CurrentUser.LogonId
                affirmationList(i).Persist()
                Return
            End If
        Next
        Dim newAffirmation As New ParticipantAffirmation(participantId, id)
        newAffirmation.IsAffirmed = isAffirmed
        newAffirmation.DateModified = System.DateTime.Now
        newAffirmation.LastModified = UserAuthorization.CurrentUser.LogonId
        newAffirmation.Persist()
        affirmationList.Add(newAffirmation)

    End Sub


    Private Sub BindCheckList()

        Me.AffirmationsCheckBoxList.DataSource = Cdm.DataCore.LookupTables.LuAffirmation.ListAll
        Me.AffirmationsCheckBoxList.DataTextField = "Description"
        Me.AffirmationsCheckBoxList.DataValueField = "AffirmationId"
        Me.AffirmationsCheckBoxList.DataBind()

    End Sub

    Private Sub SetValues(ByVal affirmationList As List(Of ParticipantAffirmation))

        Dim i As Integer
        For i = 0 To affirmationList.Count - 1
            SetSingleValue(affirmationList(i).AffirmationId, affirmationList(i).IsAffirmed)
        Next

    End Sub

    Private Sub SetSingleValue(ByVal affirmationId As Integer, ByVal isAffirmed As Boolean)

        Dim i As Integer
        Dim chkVal As Integer
        For i = 0 To Me.AffirmationsCheckBoxList.Items.Count - 1
            chkVal = Convert.ToInt32(Me.AffirmationsCheckBoxList.Items(i).Value)
            If chkVal = affirmationId Then
                AffirmationsCheckBoxList.Items(i).Selected = isAffirmed
            End If
        Next

    End Sub

    Public Sub ResetControls() Implements IPermission.ResetControls

    End Sub

    Public ReadOnly Property SectionName() As String Implements IPermission.SectionName
        Get
            Return "Affirmations"
        End Get
    End Property

End Class
